using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using SpringWaterDataAccess;
using Utilities;

namespace sow
{
	/// <summary>
	/// Summary description for EditPartner.
	/// </summary>
	public partial class EditForum : System.Web.UI.Page
	{
		protected SQLAccess sql; 
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			
			GetSource();
				
			if (!IsPostBack)
			{
				BindData();
				DataGrid1.EditItemIndex = -1;
			}
			
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
			this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
			this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);

		}
		#endregion

		protected void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			
			DataGrid1.EditItemIndex = e.Item.ItemIndex;
			BindData();			
			lblMessage.Text = "Editing!";
		}

		protected void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			
			int ForumID =(int)DataGrid1.DataKeys[(int)e.Item.ItemIndex]; 
			
			string ForumName = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
					
			string sRoleID =((TextBox)e.Item.Cells[3].Controls[0]).Text; 
			
			int RoleID = 0;
			if (sRoleID.Length != 0) 
				RoleID = int.Parse(sRoleID);
			
			/*
			lblMessage.Text = "The record: --" + PartnerID.ToString() + PartnerCode + 
				Description + URL + CreatedBy + CreatedDateTime + LastModifiedBy + 
				LastModifiedDateTime + "-- has been updated!!!<br>";
			*/

			
			if (sql == null)
				sql = new SQLAccess();
			sql.UpdateForumData(ForumID, ForumName, RoleID);

			DataGrid1.EditItemIndex = -1;
			GetSource();
			BindData();
			
			
		}

		protected void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			DataGrid1.EditItemIndex = -1;
			BindData();
			//DataGrid1.EditItemIndex = -1;
		}

		
		

		private void GetSource()
		{
			sql = new SQLAccess();
			DataGrid1.DataSource = sql.getForumData();
			DataGrid1.DataKeyField = "ForumID";
			//sqlConnection1.ConnectionString = sql.GetConnectionString();
			//sqlDataAdapter1.Fill(dsPartner1);
		}
		private void BindData()
		{
			DataGrid1.DataBind();
		}

		protected void btnAdd_Click(object sender, System.EventArgs e)
		{
			int ForumID = int.Parse(txtForumID.Text);

			int RoleID = int.Parse(txtRole.Text);

			string ForumName = txtForumName.Text;
			
			if (sql == null)
				sql = new SQLAccess();
			sql.InsertForum(ForumID, ForumName, RoleID);

			GetSource();
			BindData();
		}

		
	}
}
